<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>eventtarget.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_eventtarget.js.html">eventtarget.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A disposable implementation of a custom
<a name="line17"></a> * listenable/event target. See also: documentation for
<a name="line18"></a> * {@code goog.events.Listenable}.
<a name="line19"></a> *
<a name="line20"></a> * @author arv@google.com (Erik Arvidsson) [Original implementation]
<a name="line21"></a> * @see ../demos/eventtarget.html
<a name="line22"></a> * @see goog.events.Listenable
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line30"></a>goog.require(&#39;goog.events&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line32"></a>goog.require(&#39;goog.events.Listenable&#39;);
<a name="line33"></a>goog.require(&#39;goog.events.ListenerMap&#39;);
<a name="line34"></a>goog.require(&#39;goog.object&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * An implementation of {@code goog.events.Listenable} with full W3C
<a name="line40"></a> * EventTarget-like support (capture/bubble mechanism, stopping event
<a name="line41"></a> * propagation, preventing default actions).
<a name="line42"></a> *
<a name="line43"></a> * You may subclass this class to turn your class into a Listenable.
<a name="line44"></a> *
<a name="line45"></a> * Unless propagation is stopped, an event dispatched by an
<a name="line46"></a> * EventTarget will bubble to the parent returned by
<a name="line47"></a> * {@code getParentEventTarget}. To set the parent, call
<a name="line48"></a> * {@code setParentEventTarget}. Subclasses that don&#39;t support
<a name="line49"></a> * changing the parent can override the setter to throw an error.
<a name="line50"></a> *
<a name="line51"></a> * Example usage:
<a name="line52"></a> * &lt;pre&gt;
<a name="line53"></a> *   var source = new goog.events.EventTarget();
<a name="line54"></a> *   function handleEvent(e) {
<a name="line55"></a> *     alert(&#39;Type: &#39; + e.type + &#39;; Target: &#39; + e.target);
<a name="line56"></a> *   }
<a name="line57"></a> *   source.listen(&#39;foo&#39;, handleEvent);
<a name="line58"></a> *   // Or: goog.events.listen(source, &#39;foo&#39;, handleEvent);
<a name="line59"></a> *   ...
<a name="line60"></a> *   source.dispatchEvent(&#39;foo&#39;);  // will call handleEvent
<a name="line61"></a> *   ...
<a name="line62"></a> *   source.unlisten(&#39;foo&#39;, handleEvent);
<a name="line63"></a> *   // Or: goog.events.unlisten(source, &#39;foo&#39;, handleEvent);
<a name="line64"></a> * &lt;/pre&gt;
<a name="line65"></a> *
<a name="line66"></a> * @constructor
<a name="line67"></a> * @extends {goog.Disposable}
<a name="line68"></a> * @implements {goog.events.Listenable}
<a name="line69"></a> */
<a name="line70"></a>goog.events.EventTarget = function() {
<a name="line71"></a>  goog.Disposable.call(this);
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * Maps of event type to an array of listeners.
<a name="line75"></a>   * @private {!goog.events.ListenerMap}
<a name="line76"></a>   */
<a name="line77"></a>  this.eventTargetListeners_ = new goog.events.ListenerMap(this);
<a name="line78"></a>
<a name="line79"></a>  /**
<a name="line80"></a>   * The object to use for event.target. Useful when mixing in an
<a name="line81"></a>   * EventTarget to another object.
<a name="line82"></a>   * @private {!Object}
<a name="line83"></a>   */
<a name="line84"></a>  this.actualEventTarget_ = this;
<a name="line85"></a>};
<a name="line86"></a>goog.inherits(goog.events.EventTarget, goog.Disposable);
<a name="line87"></a>goog.events.Listenable.addImplementation(goog.events.EventTarget);
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * An artificial cap on the number of ancestors you can have. This is mainly
<a name="line92"></a> * for loop detection.
<a name="line93"></a> * @const {number}
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.events.EventTarget.MAX_ANCESTORS_ = 1000;
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Parent event target, used during event bubbling.
<a name="line101"></a> *
<a name="line102"></a> * TODO(user): Change this to goog.events.Listenable. This
<a name="line103"></a> * currently breaks people who expect getParentEventTarget to return
<a name="line104"></a> * goog.events.EventTarget.
<a name="line105"></a> *
<a name="line106"></a> * @type {goog.events.EventTarget}
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.events.EventTarget.prototype.parentEventTarget_ = null;
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Returns the parent of this event target to use for bubbling.
<a name="line114"></a> *
<a name="line115"></a> * @return {goog.events.EventTarget} The parent EventTarget or null if
<a name="line116"></a> *     there is no parent.
<a name="line117"></a> * @override
<a name="line118"></a> */
<a name="line119"></a>goog.events.EventTarget.prototype.getParentEventTarget = function() {
<a name="line120"></a>  return this.parentEventTarget_;
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Sets the parent of this event target to use for capture/bubble
<a name="line126"></a> * mechanism.
<a name="line127"></a> * @param {goog.events.EventTarget} parent Parent listenable (null if none).
<a name="line128"></a> */
<a name="line129"></a>goog.events.EventTarget.prototype.setParentEventTarget = function(parent) {
<a name="line130"></a>  this.parentEventTarget_ = parent;
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Adds an event listener to the event target. The same handler can only be
<a name="line136"></a> * added once per the type. Even if you add the same handler multiple times
<a name="line137"></a> * using the same type then it will only be called once when the event is
<a name="line138"></a> * dispatched.
<a name="line139"></a> *
<a name="line140"></a> * @param {string} type The type of the event to listen for.
<a name="line141"></a> * @param {function(?):?|{handleEvent:function(?):?}|null} handler The function
<a name="line142"></a> *     to handle the event. The handler can also be an object that implements
<a name="line143"></a> *     the handleEvent method which takes the event object as argument.
<a name="line144"></a> * @param {boolean=} opt_capture In DOM-compliant browsers, this determines
<a name="line145"></a> *     whether the listener is fired during the capture or bubble phase
<a name="line146"></a> *     of the event.
<a name="line147"></a> * @param {Object=} opt_handlerScope Object in whose scope to call
<a name="line148"></a> *     the listener.
<a name="line149"></a> * @deprecated Use {@code #listen} instead, when possible. Otherwise, use
<a name="line150"></a> *     {@code goog.events.listen} if you are passing Object
<a name="line151"></a> *     (instead of Function) as handler.
<a name="line152"></a> */
<a name="line153"></a>goog.events.EventTarget.prototype.addEventListener = function(
<a name="line154"></a>    type, handler, opt_capture, opt_handlerScope) {
<a name="line155"></a>  goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Removes an event listener from the event target. The handler must be the
<a name="line161"></a> * same object as the one added. If the handler has not been added then
<a name="line162"></a> * nothing is done.
<a name="line163"></a> *
<a name="line164"></a> * @param {string} type The type of the event to listen for.
<a name="line165"></a> * @param {function(?):?|{handleEvent:function(?):?}|null} handler The function
<a name="line166"></a> *     to handle the event. The handler can also be an object that implements
<a name="line167"></a> *     the handleEvent method which takes the event object as argument.
<a name="line168"></a> * @param {boolean=} opt_capture In DOM-compliant browsers, this determines
<a name="line169"></a> *     whether the listener is fired during the capture or bubble phase
<a name="line170"></a> *     of the event.
<a name="line171"></a> * @param {Object=} opt_handlerScope Object in whose scope to call
<a name="line172"></a> *     the listener.
<a name="line173"></a> * @deprecated Use {@code #unlisten} instead, when possible. Otherwise, use
<a name="line174"></a> *     {@code goog.events.unlisten} if you are passing Object
<a name="line175"></a> *     (instead of Function) as handler.
<a name="line176"></a> */
<a name="line177"></a>goog.events.EventTarget.prototype.removeEventListener = function(
<a name="line178"></a>    type, handler, opt_capture, opt_handlerScope) {
<a name="line179"></a>  goog.events.unlisten(this, type, handler, opt_capture, opt_handlerScope);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/** @override */
<a name="line184"></a>goog.events.EventTarget.prototype.dispatchEvent = function(e) {
<a name="line185"></a>  this.assertInitialized_();
<a name="line186"></a>
<a name="line187"></a>  var ancestorsTree, ancestor = this.getParentEventTarget();
<a name="line188"></a>  if (ancestor) {
<a name="line189"></a>    ancestorsTree = [];
<a name="line190"></a>    var ancestorCount = 1;
<a name="line191"></a>    for (; ancestor; ancestor = ancestor.getParentEventTarget()) {
<a name="line192"></a>      ancestorsTree.push(ancestor);
<a name="line193"></a>      goog.asserts.assert(
<a name="line194"></a>          (++ancestorCount &lt; goog.events.EventTarget.MAX_ANCESTORS_),
<a name="line195"></a>          &#39;infinite loop&#39;);
<a name="line196"></a>    }
<a name="line197"></a>  }
<a name="line198"></a>
<a name="line199"></a>  return goog.events.EventTarget.dispatchEventInternal_(
<a name="line200"></a>      this.actualEventTarget_, e, ancestorsTree);
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Removes listeners from this object.  Classes that extend EventTarget may
<a name="line206"></a> * need to override this method in order to remove references to DOM Elements
<a name="line207"></a> * and additional listeners.
<a name="line208"></a> * @override
<a name="line209"></a> */
<a name="line210"></a>goog.events.EventTarget.prototype.disposeInternal = function() {
<a name="line211"></a>  goog.events.EventTarget.superClass_.disposeInternal.call(this);
<a name="line212"></a>
<a name="line213"></a>  this.removeAllListeners();
<a name="line214"></a>  this.parentEventTarget_ = null;
<a name="line215"></a>};
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/** @override */
<a name="line219"></a>goog.events.EventTarget.prototype.listen = function(
<a name="line220"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line221"></a>  this.assertInitialized_();
<a name="line222"></a>  return this.eventTargetListeners_.add(
<a name="line223"></a>      String(type), listener, false /* callOnce */, opt_useCapture,
<a name="line224"></a>      opt_listenerScope);
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/** @override */
<a name="line229"></a>goog.events.EventTarget.prototype.listenOnce = function(
<a name="line230"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line231"></a>  return this.eventTargetListeners_.add(
<a name="line232"></a>      String(type), listener, true /* callOnce */, opt_useCapture,
<a name="line233"></a>      opt_listenerScope);
<a name="line234"></a>};
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>/** @override */
<a name="line238"></a>goog.events.EventTarget.prototype.unlisten = function(
<a name="line239"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line240"></a>  return this.eventTargetListeners_.remove(
<a name="line241"></a>      String(type), listener, opt_useCapture, opt_listenerScope);
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/** @override */
<a name="line246"></a>goog.events.EventTarget.prototype.unlistenByKey = function(key) {
<a name="line247"></a>  return this.eventTargetListeners_.removeByKey(key);
<a name="line248"></a>};
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>/** @override */
<a name="line252"></a>goog.events.EventTarget.prototype.removeAllListeners = function(opt_type) {
<a name="line253"></a>  // TODO(user): Previously, removeAllListeners can be called on
<a name="line254"></a>  // uninitialized EventTarget, so we preserve that behavior. We
<a name="line255"></a>  // should remove this when usages that rely on that fact are purged.
<a name="line256"></a>  if (!this.eventTargetListeners_) {
<a name="line257"></a>    return 0;
<a name="line258"></a>  }
<a name="line259"></a>  return this.eventTargetListeners_.removeAll(opt_type);
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/** @override */
<a name="line264"></a>goog.events.EventTarget.prototype.fireListeners = function(
<a name="line265"></a>    type, capture, eventObject) {
<a name="line266"></a>  // TODO(user): Original code avoids array creation when there
<a name="line267"></a>  // is no listener, so we do the same. If this optimization turns
<a name="line268"></a>  // out to be not required, we can replace this with
<a name="line269"></a>  // getListeners(type, capture) instead, which is simpler.
<a name="line270"></a>  var listenerArray = this.eventTargetListeners_.listeners[String(type)];
<a name="line271"></a>  if (!listenerArray) {
<a name="line272"></a>    return true;
<a name="line273"></a>  }
<a name="line274"></a>  listenerArray = goog.array.clone(listenerArray);
<a name="line275"></a>
<a name="line276"></a>  var rv = true;
<a name="line277"></a>  for (var i = 0; i &lt; listenerArray.length; ++i) {
<a name="line278"></a>    var listener = listenerArray[i];
<a name="line279"></a>    // We might not have a listener if the listener was removed.
<a name="line280"></a>    if (listener &amp;&amp; !listener.removed &amp;&amp; listener.capture == capture) {
<a name="line281"></a>      var listenerFn = listener.listener;
<a name="line282"></a>      var listenerHandler = listener.handler || listener.src;
<a name="line283"></a>
<a name="line284"></a>      if (listener.callOnce) {
<a name="line285"></a>        this.unlistenByKey(listener);
<a name="line286"></a>      }
<a name="line287"></a>      rv = listenerFn.call(listenerHandler, eventObject) !== false &amp;&amp; rv;
<a name="line288"></a>    }
<a name="line289"></a>  }
<a name="line290"></a>
<a name="line291"></a>  return rv &amp;&amp; eventObject.returnValue_ != false;
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/** @override */
<a name="line296"></a>goog.events.EventTarget.prototype.getListeners = function(type, capture) {
<a name="line297"></a>  return this.eventTargetListeners_.getListeners(String(type), capture);
<a name="line298"></a>};
<a name="line299"></a>
<a name="line300"></a>
<a name="line301"></a>/** @override */
<a name="line302"></a>goog.events.EventTarget.prototype.getListener = function(
<a name="line303"></a>    type, listener, capture, opt_listenerScope) {
<a name="line304"></a>  return this.eventTargetListeners_.getListener(
<a name="line305"></a>      String(type), listener, capture, opt_listenerScope);
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/** @override */
<a name="line310"></a>goog.events.EventTarget.prototype.hasListener = function(
<a name="line311"></a>    opt_type, opt_capture) {
<a name="line312"></a>  var id = goog.isDef(opt_type) ? String(opt_type) : undefined;
<a name="line313"></a>  return this.eventTargetListeners_.hasListener(id, opt_capture);
<a name="line314"></a>};
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/**
<a name="line318"></a> * Sets the target to be used for {@code event.target} when firing
<a name="line319"></a> * event. Mainly used for testing. For example, see
<a name="line320"></a> * {@code goog.testing.events.mixinListenable}.
<a name="line321"></a> * @param {!Object} target The target.
<a name="line322"></a> */
<a name="line323"></a>goog.events.EventTarget.prototype.setTargetForTesting = function(target) {
<a name="line324"></a>  this.actualEventTarget_ = target;
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Asserts that the event target instance is initialized properly.
<a name="line330"></a> * @private
<a name="line331"></a> */
<a name="line332"></a>goog.events.EventTarget.prototype.assertInitialized_ = function() {
<a name="line333"></a>  goog.asserts.assert(
<a name="line334"></a>      this.eventTargetListeners_,
<a name="line335"></a>      &#39;Event target is not initialized. Did you call the superclass &#39; +
<a name="line336"></a>      &#39;(goog.events.EventTarget) constructor?&#39;);
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Dispatches the given event on the ancestorsTree.
<a name="line342"></a> *
<a name="line343"></a> * @param {!Object} target The target to dispatch on.
<a name="line344"></a> * @param {goog.events.Event|Object|string} e The event object.
<a name="line345"></a> * @param {Array.&lt;goog.events.Listenable&gt;=} opt_ancestorsTree The ancestors
<a name="line346"></a> *     tree of the target, in reverse order from the closest ancestor
<a name="line347"></a> *     to the root event target. May be null if the target has no ancestor.
<a name="line348"></a> * @return {boolean} If anyone called preventDefault on the event object (or
<a name="line349"></a> *     if any of the listeners returns false) this will also return false.
<a name="line350"></a> * @private
<a name="line351"></a> */
<a name="line352"></a>goog.events.EventTarget.dispatchEventInternal_ = function(
<a name="line353"></a>    target, e, opt_ancestorsTree) {
<a name="line354"></a>  var type = e.type || /** @type {string} */ (e);
<a name="line355"></a>
<a name="line356"></a>  // If accepting a string or object, create a custom event object so that
<a name="line357"></a>  // preventDefault and stopPropagation work with the event.
<a name="line358"></a>  if (goog.isString(e)) {
<a name="line359"></a>    e = new goog.events.Event(e, target);
<a name="line360"></a>  } else if (!(e instanceof goog.events.Event)) {
<a name="line361"></a>    var oldEvent = e;
<a name="line362"></a>    e = new goog.events.Event(type, target);
<a name="line363"></a>    goog.object.extend(e, oldEvent);
<a name="line364"></a>  } else {
<a name="line365"></a>    e.target = e.target || target;
<a name="line366"></a>  }
<a name="line367"></a>
<a name="line368"></a>  var rv = true, currentTarget;
<a name="line369"></a>
<a name="line370"></a>  // Executes all capture listeners on the ancestors, if any.
<a name="line371"></a>  if (opt_ancestorsTree) {
<a name="line372"></a>    for (var i = opt_ancestorsTree.length - 1; !e.propagationStopped_ &amp;&amp; i &gt;= 0;
<a name="line373"></a>         i--) {
<a name="line374"></a>      currentTarget = e.currentTarget = opt_ancestorsTree[i];
<a name="line375"></a>      rv = currentTarget.fireListeners(type, true, e) &amp;&amp; rv;
<a name="line376"></a>    }
<a name="line377"></a>  }
<a name="line378"></a>
<a name="line379"></a>  // Executes capture and bubble listeners on the target.
<a name="line380"></a>  if (!e.propagationStopped_) {
<a name="line381"></a>    currentTarget = e.currentTarget = target;
<a name="line382"></a>    rv = currentTarget.fireListeners(type, true, e) &amp;&amp; rv;
<a name="line383"></a>    if (!e.propagationStopped_) {
<a name="line384"></a>      rv = currentTarget.fireListeners(type, false, e) &amp;&amp; rv;
<a name="line385"></a>    }
<a name="line386"></a>  }
<a name="line387"></a>
<a name="line388"></a>  // Executes all bubble listeners on the ancestors, if any.
<a name="line389"></a>  if (opt_ancestorsTree) {
<a name="line390"></a>    for (i = 0; !e.propagationStopped_ &amp;&amp; i &lt; opt_ancestorsTree.length; i++) {
<a name="line391"></a>      currentTarget = e.currentTarget = opt_ancestorsTree[i];
<a name="line392"></a>      rv = currentTarget.fireListeners(type, false, e) &amp;&amp; rv;
<a name="line393"></a>    }
<a name="line394"></a>  }
<a name="line395"></a>
<a name="line396"></a>  return rv;
<a name="line397"></a>};
</pre>


</body>
</html>
